<!-- 注册 -->
<template>
	<view class="register">
		<view class="register-container">
			<view class="register-li flex-start border" v-if="!isregister">
				<text>人员类型</text>
				<!-- <view class="" style="color: #a2a5ad;" >
					
				</view> -->
				<input type="text" @click="show= true" v-model="type.label" value="" disabled placeholder="请选择人员类型" />
				<u-select v-model="show" mode="single-column" :list="selectList" @confirm="confirm"></u-select>
			</view>
			
			<!-- <view class="register-li flex-start border" v-if="type.value == '04' && !isregister">
				<text>学校</text>
				<input type="text" v-model="dataBase.academyId" value="" placeholder="请输入学校" />
			</view> -->
			<view class="register-li flex-start border">
				<text>姓名</text>
				<input type="text" v-model="dataBase.name" value="" placeholder="请输入姓名" />
			</view>
			<view class="register-li flex-start border">
				<text>手机号</text>
				<input type="text" v-model="dataBase.phone" maxlength="11" value="" placeholder="请输入手机号" />
			</view>
			<view class="register-li flex-start border" v-if="!isregister && type.value == '01'">
				<text>地址（选填）</text>
				<input type="text" v-model="dataBase.address" value="" placeholder="请输入地址" />
			</view>
			<view class="register-li flex-start border" v-if="type.value == '01' && !isregister">
				<text>车牌号（选填）</text>
				<input type="text" v-model="dataBase.LicenseNumber" maxlength="11" value="" placeholder="请输入车牌号" />
			</view>
			<view class="register-li flex-start border" v-if="!isregister && type.value != '01' && type.value != '05'">
				<text>学院</text>
				<input type="text" @click="scShow= true" disabled v-model="scType.label" value="" placeholder="请选择学院" />
				<u-select value-name="id" label-name="name" v-model="scShow" mode="single-column" :list="scList" @confirm="scConfirm"></u-select>
			</view>
			<view class="register-li flex-start border" v-if="type.value == '05' && !isregister">
				<text>校门</text>
				<input type="text" @click="getDoor()" disabled v-model="doortype.label" value="" placeholder="请选择校门" />
				<u-select value-name="id" label-name="nameZh" v-model="doorShow" mode="single-column" :list="doorList" @confirm="doorConfirm"></u-select>
			</view>
			<view class="register-li flex-start border" v-if="type.value == '03' && !isregister">
				<text>班级</text>
				<input type="text" @click="setscShow()" disabled v-model="gradeIdType.label" value="" placeholder="请输入班级" />
				<u-select value-name="id" label-name="name" v-model="gradeId" mode="single-column" :list="gradeIdList" @confirm="gradeIdConfirm"></u-select>
			</view>
		</view>
		
		<view class="register-li-btn" @click="isregister?getexamine():navigate()">
			立即注册
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	/* 先主动查询是否有用户，有就直接注册成功，反之填写详细内容 */
	export default {
		data() {
			return {
				show: false,
				scShow:false,
				doorShow:false,
				isregister:true,
				selectList: [{
						value: '01',
						label: '外来人员'
					},
					{
						value: '02',
						label: '老师'
					},
					{
						value: '03',
						label: '学生'
					},
					{
						value: '04',
						label: '校内人员'
					},
					{
						value: '05',
						label: '校门'
					}
				],
				gradeId:false,
				scList:[],//学院
				gradeIdList:[],//班级
				// 01 :外来人员,02老师,03学生,04校内人员
				dataBase:{
					type:'',
					phone:'',
					name:'',
					gradeId: '',
					address:'',
					academyId: '110',
					LicenseNumber: ''
				},
				type:{},
				scType:{},
				doorList:[],
				doortype:{},
				gradeIdType:{}
			}
		},
		onLoad() {
			this.getlist()
			console.log(this.isregister)
			let isregister = this.$db.get('isregister') || true
			if(isregister){
				this.isregister = isregister
			}
			
		},
		methods: {
			// 获取校门
			getDoor(){
				this.$api.university({}).then(res=>{
					this.doorList = res.data
					this.doorShow= true
				}).catch(res=>{})
				
				
			},
			getlist(e){
				let data = {}
				if(e){
					data.parentId=e
				}
				
				this.$api.list({data:data}).then(res => {
					if(e){
						this.gradeIdList = res.data
					}else{
						this.scList = res.data
					}
				}).catch(res=>{
					// console.log(res);
				})
			},
			setscShow(){
				// this.getlist(this.dataBase.academyId)
				
				this.gradeId = true
			},
			doorConfirm(e){
				console.log(e);
				this.doortype = e[0]
				this.dataBase.doorId = e[0].value
			},
			gradeIdConfirm(e){//班级
				this.gradeIdType = e[0]
				this.dataBase.gradeId = e[0].value
			},
			confirm(e) {
				this.type = e[0]
				this.dataBase.type = e[0].value
				console.log(e);
			},
			scConfirm(e){//学院
				this.scType = e[0]
				// this.scList.forEach(res=>{
				// 	if(res.id == e[0].value){
				// 		this.dataBase.academyId =  res.pId
				// 	}
				// })
				this.dataBase.academyId = e[0].value
				// console.log(this.dataBase);
				this.getlist(e[0].value)
			},
			getexamine (){
				// 注册先检查
				let data = {
					username:this.dataBase.name,
					phoneNumber:this.dataBase.phone
				}
				console.log(this.dataBase)
				
				this.$api.query({
					data: data
				}).then(res => {
					this.$db.set('info',res.data)
					if(res.data.type == '01' || res.data.type == '03'){
						uni.redirectTo({
							url:'../userSide/index'
						})
					}
					if(res.data.type == '02' || res.data.type == '04'){
						uni.redirectTo({
							url:'../approvalForm/index'
						})
					}
					if(res.data.type == '05'){
						uni.redirectTo({
							url:'../schoolGate/index'
						})
					}
				}).catch(res=>{
					this.isregister = false
					this.showtoast('未查询到您的信息，请填写信息注册！')
				})
			},
			showtoast(title,type){
				this.$refs.uTips.show({
					title: title,
					type: type || 'error',
					duration: '2300'
				})
			},
			navigate() {
				let type = this.type.value
				let base = this.dataBase
				console.log(this.dataBase)
				// return
				if(!base.name){
					return this.$uTips('姓名不能为空！')
				}else if(!base.phone){
					return this.$uTips('手机号不能为空！')
				}
				if(type == '01'){
					this.dataBase.gradeId = '110'
					this.dataBase.academyId = '0'
				}
				this.$api.registered({
					data: this.dataBase
				}).then(res => {
					this.$db.set('info',res.data)
					// this.$db.set('authorization',res.data.authorization)
					let share = this.$db.get('share')
					let applyId = this.$db.get('applyId')
					if(share){//验证是否从随行人员进入的
						this.$navigateTo('../share?applyId='+applyId)
						// 完成注册后删除随行人员的相关信息
						this.$db.del('share')
						this.$db.del('applyId')
						this.$db.del('isregister')
						
					}
					
					
					if(res.data.userType == '01' || res.data.userType == '03'){
						uni.redirectTo({
							url:'../userSide/index'
						})
					}
					if(res.data.userType == '02' || res.data.userType == '04'){
						uni.redirectTo({
							url:'../approvalForm/index'
						})
					}
					if(res.data.userType == '05'){
						uni.redirectTo({
							url:'../schoolGate/index'
						})
					}
				}).catch(res => {
					this.$uTips(res.data.message)
					if(res.data.code == 11){
						setTimeout(res=>{
							uni.navigateBack({
								delta:1
							})
						},1500)
					}
					
				})
			}
		},
	}
</script>

<style lang="scss">
	.register {
		overflow: hidden;
	}

	.border {
		border-bottom: 4rpx solid #F8F8F8;
	}

	.register-container {
		margin: 28rpx;
		padding: 24rpx;
		border-radius: 20rpx;
		background-color: white;
	}

	.register-li {
		padding: 30rpx 0;
		font-size: 28rpx;

		text {
			color: #777986;
			width: 220rpx;
		}

		input {
			font-size: 28rpx;
		}
	}

	.register-li-btn {
		color: #fff;
		text-align: center;
		position: fixed;
		bottom: 100rpx;
		left: 24rpx;
		right: 24rpx;
		padding: 30rpx 0;
		font-size: 28rpx;
		font-weight: bold;
		border-radius: 100rpx;
		background-color: #4d7cfb;
	}
</style>
